<template>
  <div class="app-container customer-div">
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px" style="margin-top:20px;">
        <el-form-item label="商品名称：">
          <el-select
            v-model="listQuery.goodsSn"
            filterable
            remote
            clearable
            placeholder="请输入关键词"
            :remote-method="((val)=>{remoteMethod(val)})"
            :loading="goodsLoading">
            <el-option
              v-for="item in list"
              :key="item.goodsSn"
              :label="item | fifitem"
              :value="item.goodsSn">
            </el-option>
          </el-select>
            <div style="color:red">填写商品名称图表才会有数据</div>
        </el-form-item>
         <el-form-item label="日期：">
           <el-date-picker
             :picker-options="pickerOptions2"
             v-model="listQuery.timeData"
             type="daterange"
             format="yyyy 年 MM 月 dd 日"
             value-format="yyyy-MM-dd"
             range-separator="至"
             start-placeholder="开始日期"
             end-placeholder="结束日期">
           </el-date-picker>
         </el-form-item>

        <el-form-item label-width="0px">
          <el-button
            @click="handleSearchList"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>

     <div id="myChart" :style="{'height':'300px'}" ></div>
      <!--数据列表-->
      <div class="table-container">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
          <el-table-column  label="日期"  align="center" min-width="100">
            <template slot-scope="scope">
              {{scope.row.day}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="商品名称" min-width="100">
            <template slot-scope="scope">
              {{scope.row.goodsSn}}
            </template>
          </el-table-column>
          <el-table-column label="库存数量" align="center" min-width="150">
            <template slot-scope="scope">
              {{scope.row.stock}}
            </template>
          </el-table-column>
          <el-table-column min-width="100" label="价格" align="center">
            <template slot-scope="scope">
              {{scope.row.price}}
            </template>
          </el-table-column>
          <el-table-column min-width="100" label="修改价格" align="center">
            <template slot-scope="scope">
                <el-button type="text" @click="showFrom(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :current-page.sync="listQuery.pageNum"
          :page-size="listQuery.pageSize"
          :page-sizes="[10,15,20,50,100]"
          :total="count">
        </el-pagination>
      </div>

      <!--修改价格-->
      <el-dialog
        title="修改价格"
        :visible.sync="dialogVisible"
        width="420px">
        <el-form :model="form" size="mini" label-width="120px" ref="form">
          <el-form-item label="商品名称：">
                {{form.goodsSn}}
          </el-form-item>
          <el-form-item label="商品价格：">
            <el-input v-model="form.price"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>
<script>
  import {goodsStockTrend,goodsStockList,updateGoodsStock} from '@/api/skuStock'
  import {getCrmGoodsList} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    pageNum: 1,
    pageSize: 10,
    goodsSn: '',
    timeData:[],

  };
  const defaultForm = {
    gscId: '',
    goodsSn: '',
    price: '',
  }
  export default {
    name: "index",
    data(){
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        dialogVisible:false,
        btnLoading:false,
        form:JSON.parse(JSON.stringify(defaultForm)),
        goodsLoading:false,
        list:[],
        pickerOptions2: {
          shortcuts: [{
            text: '7天',
            onClick(picker) {
              let date = new Date();
              let endTime = date.setTime(date.getTime() - 3600 * 1000 * 24);
              let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
              let startTime = date.setTime(endTime - 3600 * 1000 * 24 * 7);
              let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd')
              picker.$emit('pick', [startDate, endDate]);
            },
          },{
            text: '15天',
            onClick(picker) {
              let date = new Date();
              let endTime = date.setTime(date.getTime() - 3600 * 1000 * 24);
              let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
              let startTime = date.setTime(endTime - 3600 * 1000 * 24 * 15);
              let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd')
              picker.$emit('pick', [startDate, endDate]);
            },
          },{
            text: '30天',
            onClick(picker) {
              let date = new Date();
              let endTime = date.setTime(date.getTime() - 3600 * 1000 * 24);
              let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
              let startTime = date.setTime(endTime - 3600 * 1000 * 24 * 30);
              let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd')
              picker.$emit('pick', [startDate, endDate]);
            },
          },{
            text: '60天',
            onClick(picker) {
              let date = new Date();
              let endTime = date.setTime(date.getTime() - 3600 * 1000 * 24);
              let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
              let startTime = date.setTime(endTime - 3600 * 1000 * 24  * 60);
              let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd')
              picker.$emit('pick', [startDate, endDate]);
            },
          }]
        },

      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId',
        'isMange',
      ]),


    },
    created() {
      let date = new Date();
      let endTime = date.setTime(date.getTime() - 3600 * 1000 * 24);
      let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
      let startTime = date.setTime(endTime - 3600 * 1000 * 24 * 7);
      let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd')
      this.listQuery.timeData = [startDate,endDate];
      this.getList();
      this.getTb()

    },
    filters: {
      fifitem(item){
        return item.goodsName+'/'+item.goodsSn
      },
    },
    methods:{
      //联想商品
      remoteMethod(key){
        this.goodsLoading = true;
        getCrmGoodsList({keyword:key}).then((res)=>{
          this.list = res;
          this.goodsLoading = false;
        }).catch(()=>{
          this.goodsLoading = false;
        })
      },
      showFrom(item){
        this.form = JSON.parse(JSON.stringify(defaultForm))
        this.form.gscId = item.gscId;
        this.form.goodsSn = item.goodsSn;
        this.form.price = item.price;
        this.dialogVisible = true;
      },
      handleClose(){
        this.dialogVisible = false;
      },
      handleConfirm(){
        this.btnLoading = true;
        updateGoodsStock(this.form).then((res)=>{
          this.btnLoading = false;
          this.handleClose();
          this.getList();
        }).catch(()=>{
          this.btnLoading = false;
        })
      },
      getTb(){
        console.log("8888888888")
        let data = JSON.parse(JSON.stringify(this.listQuery));
        delete data.pageNum
        delete data.pageSize
        goodsStockTrend(data).then((res)=>{
          this.draw1(res.x,res.y,res.y1);
        })
      },

      //画图表
      draw1(x,y1,y2){
        let myChart = this.$echarts.init(document.getElementById('myChart'));
        // 绘制图表
        myChart.setOption({
          tooltip: {
            trigger: 'axis',
          },
          legend:{
            data:['库存','价格'],
          },
          xAxis: {
            type: 'category',
            data: x
          },
          yAxis: [
            {
              type: 'value',
              name: '库存',
            },
            {
              type: 'value',
              name: "价格",
            },
          ],
          series: [
            {
              name:'库存',
              data: y1,
              type: 'line',
              yAxisIndex:0
            },
            {
              name:'价格',
              data: y2,
              type: 'line',
              yAxisIndex:1
            },

          ]
        });
      },

      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
        this.getTb();
      },

      //获取订单列表
      getList() {
        this.listLoading = true;
        let listQuery = JSON.parse(JSON.stringify(this.listQuery));
        goodsStockList(listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },


    },
  }
</script>

<style scoped lang="scss">

</style>
